home *** CD-ROM | disk | FTP | other *** search
/ The Amiga Classic Collection / The Amiga Classic Collection - Disc 1.iso / Business / BU11-AddressMgr.DMS / BU11-AddressMgr.adf / programming_tips_code / allocator.c next >
C/C++ Source or Header  |  1994-07-08  |  9KB  |  524 lines

  1. /* ================================================================= */
  2.  
  3. /* Module name: allocator.c */
  4.   
  5. /* ----------------------------------------------------------------- */
  6.  
  7. #define ALLOCATE_GLOBALS
  8.  
  9. #include "general.h"
  10.  
  11. extern   struct NewMenu menu1[]; 
  12.  
  13.  
  14. UBYTE *alert_message_p =  
  15.  
  16. "\x00\x4A\x14" 
  17. "********* RECOVERABLE ALERT - CANNOT RUN EXAMPLE PROGRAM *********" "\x00\x01"
  18.  
  19.  
  20. "\x00\x4A\x24" 
  21. "Version 37 or upwards of Gadtools and ASL libraries required" "\x00\x01"
  22.   
  23.   
  24. "\x0\x4A\x34" 
  25. "******* PRESS LEFT OR RIGHT MOUSE BUTTON TO CONTINUE *******" "\x00";
  26.  
  27.  
  28. #define DISPLAY_COUNT 12
  29.  
  30. UBYTE (*display_list[])() = {
  31.   
  32.         OpenInt,
  33.         OpenGraphics,       
  34.         OpenGadtools,
  35.         LockScreen,
  36.         GetVisInfo,
  37.         CreateWindow,
  38.         CreateMenu,
  39.         CreateMenuLayout,
  40.         InstallMenu,
  41.         CreateTimerReplyPort,
  42.         CreateTimerRequestBlock,
  43.         OpenTimer
  44.         
  45.         };
  46.  
  47.  
  48.  
  49. main(int argc, char *argv[])
  50.  
  51. {
  52.  
  53. UBYTE error_number=NO_ERROR;
  54.  
  55. if(!(g_resource_stack_p=CreateStack(void *))) error_number=NO_STACK; 
  56.         
  57. else  {
  58.  
  59.       /* attempt to allocate resources... */
  60.  
  61.          error_number=AllocateResource(DISPLAY_COUNT,display_list);
  62.  
  63.          if (error_number) DisplayAlert(RECOVERY_ALERT,alert_message_p,80); 
  64.  
  65.          else error_number=AmigaProg();           
  66.                     
  67.        while(!PopStack(g_resource_stack_p,g_function)) g_function(); 
  68.  
  69.        KillStack(g_resource_stack_p);
  70.      
  71.      }
  72.  
  73. return(0);
  74.  
  75. } /* Logical end of program */
  76.   
  77. /* ----------------------------------------------------------------- */
  78.  
  79. UBYTE AllocateResource(UBYTE count,UBYTE (*list[])())
  80.       
  81. {
  82.          
  83. UBYTE i, error_number;
  84.               
  85. for (i=0;i<count;i++)
  86.        
  87.      {
  88.     
  89.       if(error_number=list[i]())
  90.            
  91.       i=count; /* force exit from loop */  
  92.  
  93.      }
  94.  
  95. return(error_number);
  96.     
  97. }
  98.  
  99. /* ----------------------------------------------------------------- */
  100.  
  101.  
  102. UBYTE OpenInt(void)
  103.  
  104. {
  105.  
  106. UBYTE error_number=NO_ERROR;
  107.  
  108. if(!(IntuitionBase=(struct IntuitionBase *)
  109.      OpenLibrary("intuition.library",INTUITION_VERSION)))
  110.  
  111.         error_number=ALLOCATION_ERROR;
  112.  
  113.   else {
  114.         
  115.         g_function=CloseInt;
  116.         
  117.         PushStack(g_resource_stack_p,g_function);
  118.        
  119.        }
  120.         
  121. return(error_number);
  122.  
  123. }
  124.  
  125. /* ----------------------------------------------------------------- */
  126.  
  127. void CloseInt(void)
  128.  
  129. {
  130.  
  131. CloseLibrary((struct Library *)IntuitionBase);
  132.  
  133. }
  134.  
  135. /* ----------------------------------------------------------------- */
  136.  
  137. UBYTE OpenGraphics(void)
  138.  
  139. {
  140.  
  141. UBYTE error_number=NO_ERROR;
  142.  
  143. if(!(GfxBase=(struct GfxBase *)
  144.      OpenLibrary("graphics.library",GRAPHICS_VERSION)))
  145.  
  146. error_number=ALLOCATION_ERROR;
  147.  
  148.   else {
  149.         
  150.         g_function=CloseGraphics;
  151.         
  152.         PushStack(g_resource_stack_p,g_function);
  153.                
  154.        }
  155.  
  156. return(error_number);
  157.  
  158. }
  159.  
  160. /* ----------------------------------------------------------------- */
  161.  
  162. void CloseGraphics(void)
  163.  
  164. {
  165.         
  166. CloseLibrary((struct Library *)GfxBase);
  167.  
  168. }
  169.  
  170. /* ----------------------------------------------------------------- */
  171.  
  172. UBYTE LockScreen(void)
  173.  
  174. {
  175.  
  176. UBYTE error_number=ALLOCATION_ERROR;
  177.  
  178. if (g_public_screen_p=LockPubScreen(NULL))
  179.  
  180.        {
  181.                 
  182.         g_viewport_p=&g_public_screen_p->ViewPort;
  183.         
  184.         error_number=NO_ERROR;
  185.  
  186.         g_function=UnlockScreen;
  187.         
  188.         PushStack(g_resource_stack_p,g_function);              
  189.  
  190.        }
  191.         
  192. return(error_number);
  193.  
  194. }
  195.  
  196. /* ----------------------------------------------------------------- */
  197.  
  198. void UnlockScreen(void)
  199.  
  200. {          
  201.     
  202. UnlockPubScreen(NULL,g_public_screen_p);
  203.         
  204. }
  205.  
  206. /* ----------------------------------------------------------------- */
  207.  
  208. UBYTE CreateWindow(void)
  209.  
  210. {
  211.  
  212. UBYTE error_number=ALLOCATION_ERROR;
  213.  
  214. g_window_p=OpenWindowTags(NULL,
  215.         WA_Left,0, WA_Top,0,
  216.         WA_Width,WINDOW_WIDTH, WA_Height, WINDOW_HEIGHT,
  217.         WA_DragBar, FALSE,
  218.         WA_DepthGadget, TRUE, 
  219.         WA_CloseGadget, TRUE,
  220.         WA_SmartRefresh, TRUE,
  221.         WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_MENUPICK | 
  222.         IDCMP_ACTIVEWINDOW | IDCMP_INACTIVEWINDOW,
  223.         WA_Title, "Timer Device Example - see Paul Overaa's Programming Tips",
  224.         WA_PubScreen, g_public_screen_p,
  225.         TAG_END);
  226.         
  227. if (g_window_p)
  228.         
  229.         {
  230.                 
  231.         GT_RefreshWindow(g_window_p,NULL);
  232.  
  233.         error_number=NO_ERROR;
  234.                                
  235.         g_rastport_p=g_window_p->RPort;
  236.  
  237.         g_function=KillWindow;
  238.         
  239.         PushStack(g_resource_stack_p,g_function);
  240.                
  241.         }
  242.       
  243. return(error_number);
  244.  
  245. }
  246.  
  247. /* ----------------------------------------------------------------- */
  248.  
  249. void KillWindow(void)
  250.  
  251. {
  252.  
  253. CloseWindow(g_window_p);
  254.  
  255. }
  256.  
  257. /* ----------------------------------------------------------------- */
  258.  
  259. UBYTE OpenGadtools(void)
  260.  
  261. {
  262.  
  263. UBYTE error_number=NO_ERROR;
  264.  
  265. if(!(GadToolsBase=OpenLibrary("gadtools.library",GADTOOLS_VERSION)))
  266.  
  267.         error_number=ALLOCATION_ERROR;
  268.  
  269.   else {
  270.         
  271.         g_function=CloseGadtools;
  272.         
  273.         PushStack(g_resource_stack_p,g_function);
  274.        
  275.        }
  276.         
  277. return(error_number);
  278.  
  279. }
  280.  
  281. /* ----------------------------------------------------------------- */
  282.  
  283. void CloseGadtools(void)
  284.  
  285. {
  286.  
  287. CloseLibrary((struct Library *)GadToolsBase);
  288.  
  289. }
  290.  
  291. /* ----------------------------------------------------------------- */
  292.  
  293. UBYTE GetVisInfo(void)
  294.  
  295. {
  296.  
  297. UBYTE error_number=NO_ERROR;
  298.  
  299. if(!(g_visual_info_p=GetVisualInfo(g_public_screen_p,TAG_END)))
  300.  
  301.         error_number=ALLOCATION_ERROR;
  302.  
  303.   else {
  304.         
  305.         g_function=FreeVisInfo;
  306.         
  307.         PushStack(g_resource_stack_p,g_function);
  308.        
  309.               }
  310.         
  311. return(error_number);
  312.  
  313. }
  314.  
  315. /* ----------------------------------------------------------------- */
  316.  
  317. void FreeVisInfo(void)
  318.  
  319. {
  320.         
  321. FreeVisualInfo(g_visual_info_p);
  322.         
  323. }
  324.  
  325. /* ----------------------------------------------------------------- */
  326.  
  327. UBYTE CreateMenu(void)
  328.  
  329. {
  330.  
  331. UBYTE error_number=NO_ERROR;
  332.  
  333. if(!(g_menu_p=CreateMenus(menu1,TAG_END)))
  334.  
  335.         error_number=ALLOCATION_ERROR;
  336.  
  337.   else {
  338.         
  339.         g_function=ReleaseMenu;
  340.         
  341.         PushStack(g_resource_stack_p,g_function);
  342.        
  343.        }
  344.         
  345. return(error_number);
  346.  
  347.  
  348. }
  349.  
  350. /* ----------------------------------------------------------------- */
  351.  
  352. void ReleaseMenu(void)
  353.  
  354. {
  355.         
  356. FreeMenus(g_menu_p);
  357.         
  358. }
  359.  
  360. /* ----------------------------------------------------------------- */
  361.  
  362. UBYTE CreateMenuLayout(void)
  363.  
  364. {
  365.  
  366. UBYTE error_number=NO_ERROR;
  367.  
  368. if(!(LayoutMenus(g_menu_p,g_visual_info_p,TAG_END)))
  369.  
  370.         error_number=ALLOCATION_ERROR;
  371.  
  372.   else {
  373.         
  374.         
  375.        /* Function must be tested for success but
  376.           no deallocation operations are needed! */
  377.        
  378.        }
  379.         
  380. return(error_number);
  381.  
  382.  
  383. }
  384.  
  385. /* ----------------------------------------------------------------- */
  386.  
  387. UBYTE InstallMenu(void)
  388.  
  389. {
  390.  
  391. UBYTE error_number=NO_ERROR;
  392.  
  393. if(!(SetMenuStrip(g_window_p,g_menu_p)))
  394.  
  395.         error_number=ALLOCATION_ERROR;
  396.  
  397.   else {
  398.         
  399.         g_function=RemoveMenu;
  400.         
  401.         PushStack(g_resource_stack_p,g_function);
  402.        
  403.        }
  404.         
  405. return(error_number);
  406.  
  407. }
  408.  
  409. /* ----------------------------------------------------------------- */
  410.  
  411. void RemoveMenu(void)
  412.  
  413. {
  414.  
  415. ClearMenuStrip(g_window_p);
  416.         
  417. }
  418.  
  419. /* ----------------------------------------------------------------------- */
  420.  
  421. UBYTE CreateTimerReplyPort()
  422.  
  423. {
  424.  
  425. UBYTE error_number=NO_ERROR;
  426.         
  427. if((g_timer_reply_port_p=CreatePort(TIMERNAME,0))==NULL)
  428.  
  429.    error_number=ALLOCATION_ERROR;
  430.  
  431. else {     
  432.         
  433.       g_function=DeleteTimerReplyPort;
  434.        
  435.       PushStack(g_resource_stack_p,g_function);
  436.  
  437.       }
  438.       
  439. return(error_number);
  440.  
  441. }
  442.  
  443. /* ----------------------------------------------------------------------- */
  444. void DeleteTimerReplyPort()
  445.  
  446. {
  447.  
  448. DeletePort(g_timer_reply_port_p); 
  449.  
  450. }
  451.  
  452. /* ----------------------------------------------------------------------- */
  453. UBYTE CreateTimerRequestBlock()
  454.  
  455. {
  456.  
  457. UBYTE error_number=NO_ERROR;
  458.  
  459. g_timer_request_p=(struct timerequest *) 
  460.         CreateExtIO(g_timer_reply_port_p,sizeof(struct timerequest));
  461.  
  462. if (g_timer_request_p==NULL) error_number=ALLOCATION_ERROR;
  463.                   
  464. else {     
  465.         
  466.       g_function=DeleteTimerRequestBlock;
  467.        
  468.       PushStack(g_resource_stack_p,g_function);
  469.  
  470.       }
  471.       
  472. return(error_number);
  473.  
  474. }
  475.  
  476. /* ----------------------------------------------------------------------- */
  477.  
  478. void DeleteTimerRequestBlock()
  479.  
  480. {
  481.  
  482. DeleteExtIO((struct IORequest *)g_timer_request_p);
  483.  
  484. }
  485.  
  486. /* ----------------------------------------------------------------------- */
  487.  
  488. UBYTE OpenTimer()
  489.  
  490. {
  491.  
  492. UBYTE error_number=NO_ERROR;
  493.  
  494. if((OpenDevice(TIMERNAME,UNIT_MICROHZ,(struct IORequest *)g_timer_request_p,0))!=NULL) 
  495.  
  496.      error_number=ALLOCATION_ERROR;
  497.     
  498. else {     
  499.  
  500.       g_timer_request_p->tr_node.io_Command=TR_ADDREQUEST;
  501.         
  502.       g_function=CloseTimer;
  503.        
  504.       PushStack(g_resource_stack_p,g_function);
  505.  
  506.       }
  507.       
  508. return(error_number);
  509.  
  510. }
  511.  
  512. /* ----------------------------------------------------------------------- */
  513.  
  514. void CloseTimer()
  515.  
  516. {
  517.  
  518. CloseDevice((struct IORequest *)g_timer_request_p);
  519.  
  520.  
  521. }
  522.  
  523. /* ----------------------------------------------------------------------- */
  524.